$:.unshift File.join(File.dirname(__FILE__),'..','lib')
require 'test/unit'
require 'helpers'
require 'model'

class TcTransaction < Test::Unit::TestCase
  def test_transaction_basic
    test_amt = BigDecimal.new("12.34")
    
    tx = Helpers.transaction_simple("2008-07-15", "Checking", "Expense", test_amt)
    assert_equal(test_amt, tx.total_debits)
    assert_equal(test_amt, tx.total_credits)
    assert(tx.is_balanced?)
  end
  
  def test_transaction_modify
    tx = Transaction.new("2008-08-16")
    tx.add_debit("Checking", BigDecimal.new("33.33"))
    tx.add_debit("Cash", BigDecimal.new("11.11"))
    tx.add_credit("Expense", BigDecimal.new("44.44"))
    assert_equal(3, tx.entries.length)
    assert_equal(BigDecimal.new("44.44"), tx.total_debits)
    assert_equal(BigDecimal.new("44.44"), tx.total_credits)
    assert(tx.is_balanced?)
    
    tx.remove_entry(1)
    assert_equal(2, tx.entries.length)
    assert_equal(BigDecimal.new("33.33"), tx.total_debits)
    assert_equal(BigDecimal.new("44.44"), tx.total_credits)
    assert(!tx.is_balanced?)

    tx.adjust_entry(1, BigDecimal.new("33.33"))
    assert_equal(BigDecimal.new("33.33"), tx.total_credits)
    assert(tx.is_balanced?)
  end
end
